<?php

namespace App\Service;
use GuzzleHttp\Client;
#use Illuminate\Support\Facades\Redis as Redis;
class ApiService
{
	/**
	 *接口url
	 * @var type 
	 */
	public $url = 'http://192.168.1.186/api/v2/';
	//public $url = 'http://admin999.qiliaoapp.net/api/v2/';
	//public $url = 'http://kqtest.qiliaoapp.net/api/v2/';
	/*
	 * 数组
	 */	
	public $data = [];


	/**
	 * 添加公会搜索获取公会信息
	 * @param $title 
	 * @return type
	 */	
	public function getGuildShow($title=null){
		$newAarry = [];
		$url = $this->url.'guildShow';
		$Client = new Client();
		$this->data['query'] = ['search' => $title];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				foreach($json['data'] as $k=>$v) {
					$newAarry[$k]['id'] = $v['id'];
					$newAarry[$k]['username'] = $v['title'];
				}
				return $newAarry;
			}
		}
		return [];
	}
	/**
	 * 获取单个公会名称
	 * @param $title 
	 * @return type
	 */	
	public function getGuildTitle($gid) {
		$newAarry = [];
		$url = $this->url.'guildShow';
		$Client = new Client();
		$this->data['query'] = ['search' => ''];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				foreach($json['data'] as $k=>$v) {
					if($v['id'] == $gid) {
						$newAarry['title'] = $v['title'];
					}
					
					//$newAarry[$k]['id'] = $v['id'];
					//$newAarry[$k]['username'] = $v['title'];
				}
				return $newAarry;
			}
		}
		return [];
	}
	/**
	 * 公会对应主播
	 * @param type gid 公会id
	 * @return type
	 */
	public function getAnchor($info) {		
		$url = $this->url.'guildUserShow';
		$Client = new Client();
		$this->data['query'] = ['guild' => $info['gid'], 'search' => $info['search'], 'status' => $info['status']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				return $json['data'];
			}
		}
		return [];
	}	
	/**
	 * 礼物记录
	 * @param type gid 公会id
	 * @return type
	 */
	public function getGift($info) {		
		$url = $this->url.'guildGiftShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild'], 'search' => $info['keyword'], 'time_value' => $info['time_value']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				return $json['data'];
			}
		}
		return [];
	}
	/**
	 * 礼物记录
	 * @param type gid 公会id
	 * @return type
	 */
	public function getStamp($info) {		
		$url = $this->url.'guildStampShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild'], 'search' => $info['keyword'], 'time_value' => $info['time_value']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				return $json['data'];
			}
		}
		return [];
	}
	/**
	 * 通话记录
	 * @param type gid 公会id
	 * @return type
	 */
	public function getCall($info) {		
		$url = $this->url.'guildLiveShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild'], 'search' => $info['keyword'], 'time_value' => $info['time_value']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				return $json['data'];
			}
		}
		return [];
	}
	/**
	 * 主播收益明细
	 * @param type gid 公会id
	 * @return type
	 */
	public function getIncome($info) {		
		$url = $this->url.'guildCurrencyShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild'], 'search' => $info['keyword'], 'time_value' => $info['time_value']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				return $json['data'];
			}
		}
		return [];
	}
	/**
	 * 公会收益明细
	 * @param type gid 公会id
	 * @return type
	 */
	public function getEarning($info) {		
		$url = $this->url.'guildAllShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild'], 'start_time' => $info['start_time']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];
				foreach($json['data']['list'] as $k=>&$v) {
					$v['sum_gift'] = isset($v['sum_gift']) ? $v['sum_gift'] : 0;
					$v['sum_stamp'] = isset($v['sum_stamp']) ? $v['sum_stamp'] : 0;
					$v['sum_voice'] = isset($v['sum_voice']) ? $v['sum_voice'] : 0;
					$v['sum_live'] = isset($v['sum_live']) ? $v['sum_live'] : 0;
					$v['cale'] = 0.05;
					$v['sum_guild'] = $this->getfloatlength(((($v['sum_gift'] + $v['sum_stamp'] + $v['sum_voice'] + $v['sum_live']) * 2)/100) * $v['cale']);					
				}
				return $json['data'];
			}
		}
		return [];
	}
	/**
	 * 公会收益明细
	 * @param type gid 公会id
	 * @return type
	 */
	public function getOnlineTime($info) {		
		$url = $this->url.'guildHostShow';
		$Client = new Client();		
		$this->data['query'] = ['guild' => $info['guild']];
        $result = $Client->request('GET', $url, $this->data);
		if($result->getStatusCode() == 200) {
			$json = \GuzzleHttp\json_decode($result->getBody()->getContents(), TRUE);
			if($json['code'] == 200) {
				if(empty($json['data'])) return [];		
				return $json['data'];
			}
		}
		return [];
	}	
	/**
     * Author: only
     * Date: 2019年1月22日
     * Description: 保留两位小数
     * @param $num
     * @return string
     */
    public function getfloatlength($num)
    {
        $count = 0;
        $temp = explode ( '.', $num );
        if (sizeof ( $temp ) > 1) {
            $decimal = end ( $temp );
            $count = strlen ( $decimal );
        }
        if($count >= 3)
        {
            $p = stripos($num, '.');
            $num = substr($num, 0, $p+3);
        }
        return $num;
    }
	
}